package com.vaadin.thomas.contenttool.ui;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.vaadin.thomas.contenttool.data.Tag;
import com.vaadin.thomas.contenttool.data.Value;
import com.vaadin.thomas.contenttool.ui.events.LangChangedEvent;
import com.vaadin.thomas.contenttool.ui.events.LangChangedListener;
import com.vaadin.thomas.contenttool.ui.events.MenuFragmentSelectedEvent;
import com.vaadin.thomas.contenttool.ui.events.MenuFragmentSelectedListener;
import com.vaadin.thomas.contenttool.ui.events.MenuTagSelectedEvent;
import com.vaadin.thomas.contenttool.ui.events.MenuTagSelectedListener;
import com.vaadin.thomas.contenttool.ui.events.PreviewCommittedEvent;
import com.vaadin.thomas.contenttool.ui.events.PreviewCommittedListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;

public class Content extends CssLayout implements MenuFragmentSelectedListener,
		MenuTagSelectedListener, LangChangedListener, PreviewCommittedListener {

	private static final long serialVersionUID = -2964638681436551609L;
	private Label caption;

	private String fragment;

	public Content() {
		addStyleName("content");
		setWidth(100, UNITS_PERCENTAGE);
		setMargin(false, false, true, false);

		fill((String) null);

		AdminMainLayout.bb().addListener(this);
		langChanged(null);
	}

	private void fill(String fragment) {

		this.fragment = fragment;

		// TODO optimize
		List<Value> vals = new LinkedList<Value>();

		for (Value v : AdminMainLayout.ct().getCurrentVersion().getAllValues()
				.values()) {
			if (fragment == null || v.getKey().startsWith(fragment)) {
				vals.add(v);
			}
		}

		fill(vals);
	}

	private void fill(Tag tag) {

		fragment = tag == null ? null : tag.getValue();

		// TODO optimize
		List<Value> vals = new LinkedList<Value>();

		for (Value v : AdminMainLayout.ct().getCurrentVersion().getAllValues()
				.values()) {
			if (fragment == null || v.getTags().contains(tag)) {
				vals.add(v);
			}
		}

		fill(vals);
	}

	private void fill(List<Value> vals) {

		boolean editable = AdminMainLayout.ct().isPreviewMode();
		Util.filterCTIdentifiers(vals, AdminMainLayout.ct().conf()
				.showContentToolIdentifiers());
		Collections.sort(vals);

		removeAllComponents();

		caption = new Label();
		caption.addStyleName("caption");
		addComponent(caption);

		ExpandableValueComponent last = null;
		for (Value val : vals) {
			addComponent(last = new ExpandableValueComponent(val, editable));
		}

		if (vals.size() == 1) {
			last.expand();
		}

	}

	@Override
	public void itemSelected(MenuFragmentSelectedEvent event) {
		fill(event.getIdFragment());
		langChanged(null);
	}

	@Override
	public void langChanged(LangChangedEvent event) {

		if (fragment == null) {
			caption.setValue(AdminMainLayout.ct().get(
					"contenttool.content.caption-all"));
		} else {
			caption.setValue(AdminMainLayout.ct().get(
					"contenttool.content.caption-fragment")
					+ " " + fragment);
		}
	}

	@Override
	public void previewCommited(PreviewCommittedEvent event) {
		fill(fragment);
	}

	@Override
	public void itemSelected(MenuTagSelectedEvent event) {
		fill(event.getTag());
		langChanged(null);
	}
}
